#include "CStringRes.h"
#include "CHashTable.h"

using namespace cocos2d;

CStringRes::CStringRes(void)
{
	m_pHash = NULL;
}

CStringRes::~CStringRes(void)
{
	if (m_pHash) delete m_pHash;
}

const char * CStringRes::valueForKey(const char *key, CCDictionary *dict)
{
	if (dict)
	{
		CCString *pString = (CCString*)dict->objectForKey(std::string(key));
		return pString ? pString->m_sString.c_str() : "";
	}
	return "";
}

bool CStringRes::LoadResource(const char *pszPlist)
{
	std::string pszPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszPlist);
	CCDictionary *dict = CCDictionary::createWithContentsOfFileThreadSafe(pszPath.c_str());
	CCDictionary* stringsDict = (CCDictionary*)dict->objectForKey("strings");
	int size = stringsDict->count();

	m_pHash = new CHashTable(size * 3 / 2);
	/*
	stringsDict->begin();
	CCDictionary *stringDict = NULL;
	std::string key = "";
	while ((stringDict = (CCDictionary *)stringsDict->next(&key)))
	{
		const char *value = valueForKey("value", stringDict);
		m_pHash->Add((char *)key.c_str(), (char *)value, true);
	}
	*/

	CCDictElement* pElement;
	CCDICT_FOREACH(stringsDict, pElement)
	{
		const char*key = pElement->getStrKey();
		const char *value = valueForKey("value", (CCDictionary *)pElement->getObject());
		m_pHash->Add((char *)key, (char *)value, true);
	}

	dict->release();
	return true;
}

char* CStringRes::Get(const char *pKey)
{
	return m_pHash->Get((char *)pKey);
}
